package com.vcs.frm.web.company;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.vcs.frm.domain.Employee;
import com.vcs.frm.service.company.EmployeeService;
import com.vcs.frm.web.form.emp.EmployeeRegFormBean;

@Controller
@RequestMapping(value = "/comp")
public class EmployeeController {

	private final Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private EmployeeService employeeService;
	
	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	
	@RequestMapping(value = "emp/reg", method = RequestMethod.GET)
	public ModelAndView prepareEmployeeRegistrationForm() {

		ModelAndView mav = new ModelAndView();

		EmployeeRegFormBean formBean = new EmployeeRegFormBean();
		mav.addObject("empRegFrm", formBean);
		mav.setViewName("empRegForm");
		return mav;
	}

	@RequestMapping(value = "emp/reg", method = RequestMethod.POST)
	public ModelAndView processEmployeeRegistrationForm(
			@ModelAttribute("jobseekerRegForm") EmployeeRegFormBean formBean,
			BindingResult bindingResult, Model model) {

		ModelAndView mav = new ModelAndView();
		if (bindingResult.hasErrors()) {
			mav.setViewName("empRegForm");
			return mav;
		}

		/* Save employee */
		employeeService.saveEmployee(formBean);	
		mav.setViewName("redirect:/home");
		return mav;
	}

	@RequestMapping(value = "/emp/profile")
	public ModelAndView showEmployeeProfile(HttpServletRequest request) throws Exception{
		log.info("method - showEmployeeProfile");
		
		ModelAndView mav = new ModelAndView();
		
		Employee emp = (Employee)request.getSession().getAttribute("emp");
		if(emp == null){
			throw new Exception("employee was not found in session.. This is an illegal attempt to view Employees profile");
		}
		mav.setViewName("showEmpProfile");
		mav.addObject("emp", emp);
		return mav;
	}
	
	
	@RequestMapping(value = "emp/list", method = RequestMethod.GET)
	public ModelAndView listEmployees(HttpServletRequest request) {

		ModelAndView mav = new ModelAndView();
		HttpSession session = request.getSession();
		// get company's id from session and list all employees under it.

		mav.setViewName("listEmployees");
		return mav;
	}

	@RequestMapping(value = "emp/{employeeId}", method = RequestMethod.GET)
	public ModelAndView showDetailedEmployeeProfile(
			@PathVariable("employeeId") Long employeeId,
			HttpServletRequest request) {

		ModelAndView mav = new ModelAndView();
		HttpSession session = request.getSession();
		System.out.println("show profile for employee id : " + employeeId);
		// get employee based on id and show his profile.

		mav.setViewName("showEmpProfile");
		return mav;
	}
}
